<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/3 0003
 * Time: 10:21
 */
//创建server对象，监听127.0.0.1:9501端口
$serv = new swoole_server("127.0.0.1", 9501);

$serv->set([
    'worker_num' => 4,  //worker的进程数  cpu 1-4
    'max_request' => 10000
]);

//监听连接进入事件
/**
 * $fd 客户端连接的唯一标识
 * $reactor_id 线程id
 */
$serv->on('connect', function($serv, $fd, $reactor_id){
    echo "Client: 线程id-{$reactor_id}, 客户端标识-{$fd} - Connect.\n";
});

//监听数据接收事件
$serv->on('receive', function($serv, $fd, $reactor_id, $data){
    $serv->send($fd, "Server: 线程id-{$reactor_id}, 客户端标识-{$fd} - ".$data."\n");
});

//监听连接关闭事件
$serv->on('close', function($serv, $fd){
    echo "Clinet: - $fd - Close.\n";
});

//启动服务器
$serv->start();



















